package com.dianhun.es;

import com.itextpdf.text.pdf.BaseFont;
import com.sun.media.sound.SoftTuning;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

/**
 * @author cyt
 * @create 2020-08-27 11:09
 */
@SpringBootTest
public class FrontApplication {
    @Test
    public void test() throws IOException {
        //http://xiaoyu.pep.com.cn/xxsx_176803/xxsxwd/202007/t20200727_1952809.html
        //https://xiaoyu.pep.com.cn/xxsx_176803/xxsxwd/202007/W020200727643773066455.jpg

        String imgURL = "https://xiaoyu.pep.com.cn/xxsx_176803/xxsxwd/202007/";
        String Key = "1952809";
        String url = "http://xiaoyu.pep.com.cn/xxsx_176803/xxsxwd/202007/t20200727_" + Key + ".html";
        Document parse = Jsoup.parse(new URL(url), 300000);

        Elements custom_unionStyle = parse.getElementsByClass("Custom_UnionStyle");
        Element element = custom_unionStyle.get(0);
        Elements p = element.getElementsByTag("p");
        for (Element str : p) {
            System.out.println(str.text());
        }
        Elements img = element.getElementsByTag("img");
        for (Element element1 : img) {
            String src = img.eq(0).attr("src");
            System.out.println(src);
            // System.out.println(element1);
        }
        //      System.out.println(custom_unionStyle);
        //      System.out.println(custom_unionStyle.text());
    }








    @Test
    public void parseHtmlToPdf() throws Exception {
        String imgURL = "https://xiaoyu.pep.com.cn/xxsx_176803/xxsxwd/202007/";
        String Key = "1952809";
        String url = "http://xiaoyu.pep.com.cn/xxsx_176803/xxsxwd/202007/t20200727_" + Key + ".html";
        Document parse = Jsoup.parse(new URL(url), 300000);

        System.out.println(parse);
        Elements custom_unionStyle = parse.getElementsByClass("Custom_UnionStyle");
        Element element = custom_unionStyle.get(0);
        Elements p = element.getElementsByTag("p");

        StringBuilder builder = new StringBuilder();

        for (Element str : p) {
            System.out.println(str.text());
            builder.append(str.text());
        }
        String text = element.text();
        // step 1
        String outputFile = "E:/htmlToPdf2.pdf";
        String inputFile = "F:/iText/index.html";
        // String url = new File(inputFile).toURI().toURL().toString();
        // step 2
        OutputStream os = new FileOutputStream(outputFile);
        org.xhtmlrenderer.pdf.ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(url);
        // step 3 解决中文支持
        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont("D:\\simsun\\simsun.ttc",
                BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        // 解决图片的相对路径问题
        renderer.getSharedContext().setBaseURL("https://xiaoyu.pep.com.cn/xxsx_176803/xxsxwd/202007/");
        renderer.layout();
        renderer.createPDF(os);
        os.close();
    }
}
